package com.carkeeper.subject.service.impl;

import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.subject.dao.StoreMapper;

import com.carkeeper.subject.dao.StoreUserMapper;
import com.carkeeper.subject.pojo.Store;
import com.carkeeper.subject.pojo.StoreUser;
import com.carkeeper.subject.service.AuditService;
import com.carkeeper.subject.service.StoreUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


import java.util.Map;

/**
 * @author chengxihua
 * @version 1.0
 * @date 2020/11/25 16:02
 */
@Service
public class StoreUserServiceImpl implements StoreUserService {

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private StoreUserMapper storeUserMapper;

    @Autowired
    private AuditService auditService;


    /**
     * 查询审核状态成功信息
     *
     * @return
     */
    @Override
    public Page<Store> findStoreSuccessByAuditStatus(Map<String, Object> searMap, int page, int size) {
        page = auditService.judgePageParameter(page);
        size = auditService.judgePageParameter(size);
        PageHelper.startPage(page, size);


        searMap.put("auditStatus", StatusCode.STORE_status_YES);
        Example example = createExample(searMap);

        return (Page<Store>) storeMapper.selectByExample(example);
    }

    /**
     * 创建管理员
     *
     * @param storeUser
     * @return
     */
    @Override
    public int insertStoreUser(StoreUser storeUser) {
        storeUser.setIsAdmin(StatusCode.IS_ADMIN_YES + "");
        System.out.println("创建用户成功,您的用户名为:" + storeUser.getUsername() + ",密码为:" + storeUser.getPassword());
        return storeUserMapper.insert(storeUser);
    }

    /**
     * 查询用户信息
     *
     * @return
     */
    @Override
    public Page<StoreUser> findAllStoreUser(Map<String, Object> searMap, int page, int size) {
        page = auditService.judgePageParameter(page);
        size = auditService.judgePageParameter(size);
        PageHelper.startPage(page, size);


        searMap.put("auditStatus", StatusCode.STORE_status_YES);
        Example example = createUserExample(searMap);

        return (Page<StoreUser>) storeUserMapper.selectByExample(example);
    }


    /**
     * 重置密码
     *
     * @param id
     * @return
     */
    public int resetPasswordsByUserId(Long id, String password) throws Exception {
        StoreUser storeUser = storeUserMapper.selectByPrimaryKey(id);
        String userPassword = storeUser.getPassword();
        if (password != userPassword) {
            throw new Exception("您输入的密码不正确，无法重置");
        }
        storeUser.setPassword("123456");
        int count = storeUserMapper.updateByPrimaryKey(storeUser);
        System.out.println("重置密码成功,新密码为123456");
        return count;
    }

    /**
     * 锁定用户
     *
     * @param id
     */
    public int lockUser(Long id) {
        StoreUser storeUser = storeUserMapper.selectByPrimaryKey(id);
        if ((StatusCode.IS_LOCK_NO + "").equals(storeUser.getStatus()))
            storeUser.setStatus(StatusCode.IS_LOCK_YES + "");
        else {
            storeUser.setStatus(StatusCode.IS_LOCK_NO + "");
        }
        return storeUserMapper.updateByPrimaryKey(storeUser);
    }


    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Store.class);
        Example.Criteria criteria = example.createCriteria();
        if (criteria != null) {
            if (searchMap.get("simpleName") != null && !"".equals(searchMap.get("simpleName"))) {
                criteria.andLike("simpleName", "%" + searchMap.get("simpleName") + "%");
            }
            if (searchMap.get("code") != null && !"".equals(searchMap.get("code"))) {
                criteria.andLike("code", "%" + searchMap.get("code") + "%");
            }
            if (searchMap.get("type") != null) {
                criteria.andEqualTo("type", searchMap.get("type"));
            }
            if (searchMap.get("province") != null) {
                criteria.andEqualTo("province", searchMap.get("province"));
            }
            if (searchMap.get("city") != null) {
                criteria.andEqualTo("city", searchMap.get("city"));
            }
            if (searchMap.get("county") != null) {
                criteria.andEqualTo("county", searchMap.get("county"));
            }
            if (searchMap.get("auditStatus") != null) {
                criteria.andEqualTo("auditStatus", searchMap.get("auditStatus"));
            }
        }
        return example;
    }


    /**
     * 构建用户查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createUserExample(Map<String, Object> searchMap) {
        Example example = new Example(StoreUser.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {

            if (searchMap.get("username") != null && !"".equals(searchMap.get("username"))) {
                criteria.andLike("username", "%" + searchMap.get("username") + "%");
            }
            if (searchMap.get("realname") != null && !"".equals(searchMap.get("realname"))) {
                criteria.andLike("realname", "%" + searchMap.get("realname") + "%");
            }
            if (searchMap.get("phone") != null && !"".equals(searchMap.get("phone"))) {
                criteria.andLike("phone", "%" + searchMap.get("phone") + "%");
            }
        }
        return example;
    }

}
